<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>动态组件与异步组件</title>
    <script src="../vue.global.js"></script>
</head>

<body>
    <div id="root"></div>
    <script>
        let bo = false;
        function p1() {
            return new Promise((resolve, reject) => {
                console.log("p1 resolved");
                resolve();
            })
        }

        function p2() {
            return new Promise((resolve, reject) => {
                if (bo) { 
                    console.log("p2 resolved");
                    resolve();
                }else{
                    console.log("p2 rejected");
                    reject("456");
                }
            })
        }

        function p3() {
            return new Promise((resolve, reject) => {
                console.log("p3 resolved");
                resolve("p3 resolved");
            });
        }

        // p1().then(p2).then(p3).catch(err => console.log("err: " + err));


        // const promise = new  Promise((resolve,reject)=>{
        //     console.log("执行...");
        //     resolve("执行失败");
        // })
        // promise中的resolve可以接收一个新的promise对象
        // Promise.resolve(promise).catch(err => console.log("err: " + err));

        // 测试请求
        function fetchData(){
            return new Promise((resolve,reject)=>{
                if(bo){
                    const result = {data:'User data', msg: '执行成功',code: 200};
                    resolve(result);
                }else{
                    const result = {data:'User data', msg: '执行失败',code: 500};
                    reject(result);
                }
            });
        }

        fetchData().then((res)=>{
            let {data,msg,code} = res
            console.log("data: " + data);
            console.log("msg: " + msg);
            console.log("code: " + code);
        }).catch(err =>{
            console.log("发生异常: ", err);
        })
    </script>
</body>

</html>